<?php
namespace app\api\controller;

use app\common\controller\IndexBaseController;
use think\Db;
/**
 * 钱包
 */
class WalletController extends IndexBaseController {


	

	protected function _initialize() {
		parent::_initialize();
		
	}
	
	
	public function index(){
		$list = Db::name('member')->field('credit3,credit2,credit1')-> where('id', input('uid')) -> find();
		return $this -> toJson('200', '', $list);
	}
	
	
	//充值
	public function chongz(){
		if(input('money')){
			$money=input('money');
			$member=$data = ['uid'=>input('uid'),'price' => $money,'zf'=>'+'];
			Db::name('mall_paylog')->insert($data);
			
			$list = Db::name('member')->field('credit2')-> where('id', input('uid')) -> find();
			$money=$money+$list['credit2'];
			$list1=Db::name('member')->where('id',input('uid'))->setField('credit2', $money);
			return $this -> toJson('200', '充值成功', $member);
			
		}else{
			$list = Db::name('member')->field('mobile')-> where('id', input('uid')) -> find();
		return $this -> toJson('200', '', $list);
			
		}
		
	}
	
	
	//提现
	public function gettixian(){
		$tx=Db::name("member")->alias('a')
		 ->join('mall_cash b','a.id = b.uid','LEFT')
		 ->field('b.bank_account,b.card_no,a.credit2')->where("a.id",input("uid"))->find();
	     return $this->toJson(200,'',$tx);
	}
	
	//用户余额记录
	function yue_jilu(){
		$credits_list = Db::name('mall_credits')
						->where('uid',input('uid'))
						->where('credittype','credit2')
						->where('type','in','1,2')
						->order('add_time asc')->select();
		
		if($credits_list){
			
		}else{
			return $this->toJson(402,'暂无记录');
		}
	}
	//用户积分记录
	function user_jifeng(){
		
		$credits_list = Db::name('mall_credits')->where('uid',input('uid'))->where('credittype','credit1')->order('add_time asc')->select();
		
		if($credits_list){
			
			for($i=0;$i<count($credits_list);$i++){
				
				if($i==0){
					$credits_list[$i]['balance'] = $credits_list[$i]['price'];
				}else{
					$credits_list[$i]['balance'] = $credits_list[$i-1]['balance'] + $credits_list[$i]['price'];
				}
			}
			return $this->toJson(200,'',$credits_list);
		}else{
			return $this->toJson(402,'暂无记录');
		}
	}
	//积分消费
	public function jifen(){
		$tx=Db::name("mall_score")->alias('a')
		 ->join('mall_agent b','b.id = a.agent_id','LEFT')
		 ->field('a.*,b.store_name')
		 ->where("a.user_id",input("uid"))->select();
	     return $this->toJson(200,'',$tx);
		
		
	}
	//商家积分
	
	public function shangjif(){
			$field = 'm.*,b.store_name';	
			$list=Db::name("mall_consmember")->alias('m')
			->join('mall_agent b','m.agent_id=b.id')
			->field($field)
			->where('m.u_id',input("uid"))
			->select();

		return $this->toJson(200,'',$list);
	}	
	/*
	 * 用户积分列表
	 */
	public function memberjifen(){
		$u_id = request() -> param('uid');
		$list=Db::name("line_exchangelog")->where('line_member_id',$u_id)->select();

		return $this->toJson(200,'',$list);
	}
	
	/*
	 * 提现记录表
	 */
	public function tixianjilu(){
		$uid = request() -> param('uid');
		$list=Db::name("mall_cashmanage")->where('uid',$uid)->where('cash_status',2)->select();
		/*foreach($list as $k=>$v){
			$data[]=iunserializer($v['message']);
		}
		//$a=iunserializer($site['value']);
*/
		return $this->toJson(200,'',$list);
	}
//提现中
  public function tixianz(){
  	$money=input('zmoney') - input('money');
  	$member=$data = ['uid'=>input('uid'),'price' => input('money'),'add_time'=>input('time'),'zf'=>'-','title'=>'提现中'];
	Db::name('mall_paylog')->insert($data);
  	$list1=Db::name('member')->where('id',input('uid'))->setField('credit2', $money);
  	return $this -> toJson('200', '提现中');
  	
  }
  
  //余额变化
  
  public function xiaofei(){
  		$xiaofei=Db::name('mall_corepaylog')->where('uid',input('uid'))->select();//已完成

		return json(['xiaofei'=>$xiaofei]);
  	
  	
  }
  	
  //获取收款方数据
  		public function shou(){
  			$list=Db::name('member')->where('id',input('uid'))->find();
  			return $this->toJson(200,'',$list);
  			
  		}
  	
}
